<template>
  <div class="detail_box">
    <!-- 操作 -->
    <div class="operate_box">
      <div class="item_box">
        <div class="operate_item">
          <label>设备名称：</label>
          <el-input type="text" placeholder="请输入设备名称" v-model="keyword"></el-input>
        </div>
        <div class="operate_item">
          <label>服务器：</label>
          <el-select
            v-model="server"
            placeholder="请选择服务器">
            <el-option
              v-for="(item,index) in serverOptions"
              :key="index"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div class="operate_item">
          <span class="operate_btn search_btn" @click="getTableInfo(1)">查询</span>
          <span class="operate_btn reset_btn" @click="resetTable">重置</span>
        </div>
      </div>
      <div class="btn_box">
        <span class="operate_btn search_btn" @click="confirmEdit">绑定设备</span>
      </div>
    </div>
    <!-- 表格 -->
    <div class="table_box">
      <el-table
        border
        v-loading="loading"
        :header-cell-style="tableHeader"
        ref="multipleTable"
        :data="tableInfo"
        height="430"
        tooltip-effect="dark"
        :row-key="getRowKeys"
        @selection-change="selectionChange"
        style="width: 100%;">
        <el-table-column
          type="selection"
          :reserve-selection="true"
          width="55">
        </el-table-column>
        <el-table-column
          width="80"
          prop="id"
          label="ID">
        </el-table-column>
        <el-table-column
          prop="createTime"
          sortable
          width="170"
          label="修改时间">
          <template slot-scope="scope">{{ formatDate(scope.row.modifyTime) }}</template>
        </el-table-column>
        <el-table-column
          label="设备名称">
          <template slot-scope="scope">{{ scope.row.deviceName ? scope.row.deviceName : '——' }}</template>
        </el-table-column>
        <el-table-column
          width="170"
          label="部门">
          <template slot-scope="scope">{{ scope.row.depName ? scope.row.depName : '——' }}</template>
        </el-table-column>
        <el-table-column
          prop="channel"
          label="渠道">
          <template slot-scope="scope">{{ scope.row.channel ? scope.row.channel : '——' }}</template>
        </el-table-column>
        <el-table-column
          width="130"
          label="服务器名称">
          <template slot-scope="scope">{{ scope.row.serverName ? scope.row.serverName : '——' }}</template>
        </el-table-column>
        <el-table-column
          width="100"
          label="经度">
          <template slot-scope="scope">{{ scope.row.lng ? scope.row.lng : '——' }}</template>
        </el-table-column>
        <el-table-column
          width="100"
          label="纬度">
          <template slot-scope="scope">{{ scope.row.lat ? scope.row.lat : '——' }}</template>
        </el-table-column>
      </el-table>
    </div>
    <div class="record_page">
      <span class="page_count">共<i>{{ totals }}</i>条</span>
      <el-pagination
        @size-change="sizeChange"
        @current-change="getTableInfo"
        background
        :current-page="currentPage"
        :page-sizes="[15, 20, 30, 40]"
        :page-size="pageSize"
        layout="prev, pager, next, sizes,jumper"
        :total="totals">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import commons from "@/utils/common";
import {getMonitorDevice} from "../../../api/monitor";
import {getMonitorAccount} from "../../../api/monitor/account";
import {getBindMonitorIds, mainframeBindMonitorDevice} from "../../../api/mainframe";

export default {
  props: ['deviceId'],
  data() {
    return {
      keyword: '',
      server: '',
      serverOptions: [],
      tableInfo: [],
      currentPage: 1,
      pageSize: 10,
      totals: 0,
      loading: false,
      selectItem: '',
      selectArr: []
    }
  },
  created() {
    console.log(this.deviceId)
    this.getIds();
  },
  mounted() {
    this.getServer();
    this.getTableInfo(1);
  },

  methods: {
    getTableInfo(current) {
      let that = this;
      that.loading = true;
      that.currentPage = current;
      let queryParams = {
        limit: that.pageSize,
        page: current,
        accountId: that.server,
        deviceKeyword: that.keyword,
        depKeyword: '',
        userKeyword: '',
        isBindStrategy: '',
      }
      getMonitorDevice(queryParams).then(res => {
        that.tableInfo = res.data ? res.data.list : [];
        that.totals = res.data ? res.data.count : 0;
        if (res.success) {
          that.formatIds(that.tableInfo);
        }
        that.loading = false;
      }, (err) => {
        that.loading = false;
      });
    },

    resetTable() {
      this.keyword = '';
      this.server = '';
      this.pageSize = 10;
      this.getTableInfo(1)
    },

    getServer() {
      getMonitorAccount({name: ''}).then(res => {
        this.serverOptions = res.data ? res.data : [];
      }, (err) => {
        this.serverOptions = [];
      });
    },

    getIds() {
      let that = this;
      getBindMonitorIds({recordCode: (that.deviceId ? parseFloat(that.deviceId) : ''), deviceType: 1}).then(res => {
        if (res.success && res.data) {
          that.selectArr = res.data;
          that.selectionChange(res.data)
        } else {
          that.selectItem = '';
        }
      });
    },

    formatIds(arr) {
      let that = this;
      if (that.selectArr) {
        that.selectArr.forEach((id) => {
          arr.forEach((item) => {
            if (item.id === id) {
              that.$refs.multipleTable.toggleRowSelection(item);
            }
          })
        })
      }
    },

    // 获取选中列的id
    getRowKeys(row) {
      return row.id;
    },

    // 多选
    selectionChange(val) {
      let that = this;
      let data = '';
      val.forEach((item, i) => {
        data = data + (i > 0 ? ',' : '') + item.id;
      });
      that.selectItem = data;
    },

    confirmEdit() {
      let that = this;
      that.$confirm((that.selectItem === '' ? '当前未绑定任何设备，确定进行该操作吗？' : '确定绑定设备吗？'), '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.bindEvents()
      }).catch(() => {
      });
    },

    bindEvents() {
      let that = this;
      that.loading = true;
      let params = {
        recordCode: this.deviceId,
        monitorDeviceIds: that.selectItem,
        deviceType: 1
      }
      mainframeBindMonitorDevice(params).then(res => {
        if (res.success) {
          that.closeTip(true);
          that.$message.success('操作成功！');
        }
        that.loading = false;
      }, (err) => {
        that.loading = false;
      });
    },

    closeTip(refresh) {
      this.$emit('listenToChild', {refresh: refresh})
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1);
    },

    formatDate(time) {
      return commons.formatTime(time);
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style>
.el-dialog__body {
  padding: 0 20px 20px !important;
}
</style>
<style scoped>
.detail_box {
  position: relative;
  width: 100%;
  box-sizing: border-box;
  min-width: 0;
  height: 600px;
  min-height: 600px;
  padding: 0;
}

</style>

